package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fcgzs.springboot.mapper.MemberMapper;
import com.fcgzs.springboot.mapper.OrderMapper;
import com.fcgzs.springboot.mapper.SetmealMapper;
import com.fcgzs.springboot.pojo.Member;
import com.fcgzs.springboot.pojo.Order;
import com.fcgzs.springboot.pojo.Setmeal;
import com.fcgzs.springboot.service.ReportService;
import com.fcgzs.springboot.util.DateUtils;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private MemberMapper memberMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private OrderMapper orderMapper;

    //会员统计、套餐统计、运营数据统计、运营数据导出
    @Override
    public Result getMemberReport() {
        //key:months
        //key:memberCount
        Map<String, Object> map = new HashMap<>();

        //months
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //当前日期-12月
        calendar.add(Calendar.MONTH,-12); //2020-11
        List<String> months = new ArrayList<>();
        //遍历
        for(int i = 1; i < 12; i++){
            calendar.add(Calendar.MONTH,1); //2020-12
            Date time = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(time));
        }
        map.put("months",months);

        //memberCount
        List<Integer> memberCount = new ArrayList<>();
        //遍历
        for(String date : months){
            String startTime = date + "-01";
            String endTime = date + "-31";
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.between("regTime",startTime,endTime);
            Integer integer = memberMapper.selectCount(queryWrapper);
            memberCount.add(integer);
        }
        map.put("memberCount",memberCount);
        return new Result(true,"已获取会员统计数据",map);
    }

    @Override
    public Result getSetmealReport() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> setmealCount = new ArrayList<>();
        //套餐名称
        List<String> setmealNames = new ArrayList<>();
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("setmeal_id");
        List<Map<String, Object>> maps = orderMapper.selectMaps(queryWrapper);
        //各套餐预约量
        for(Map<String,Object> map : maps){
            QueryWrapper<Order> newqueryWrapper = new QueryWrapper<>();
            newqueryWrapper.eq("setmeal_id",map.get("setmeal_id"));
            Integer integer = orderMapper.selectCount(newqueryWrapper);

            Setmeal setmeal = setmealMapper.selectById((Integer) map.get("setmeal_id"));
            setmealNames.add(setmeal.getName());

            Map<String, Object> map1 = new HashMap<>();
            map1.put("name",setmeal.getName());
            map1.put("value",integer);
            setmealCount.add(map1);
        }
        resultMap.put("setmealNames",setmealNames);
        resultMap.put("setmealCount",setmealCount);
        return new Result(true,"",resultMap);
    }

    @Override
    public Result getBusinessReportData() {
        Map<String,Object> resultMap = new HashMap<>();
        String today = null;//今日
        String thisWeekMonday = null;
        String day4ThisMonth = null;
        try {
            today = DateUtils.parseDate2String(DateUtils.getToday());
            thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());//本周一的日期
            day4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());//本月的1号
            //报告日期
            resultMap.put("reportDate",today);
            //今日新增会员数
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("regTime",today);
            Integer integer = memberMapper.selectCount(queryWrapper);
            resultMap.put("todayNewMember",integer);

            //总会员数
            Integer integer1 = memberMapper.selectCount(null);
            resultMap.put("totalMember",integer1);

            //本周新增会员数
            QueryWrapper<Member> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.between("regTime",thisWeekMonday,today);
            Integer integer2 = memberMapper.selectCount(queryWrapper1);
            resultMap.put("thisWeekNewMember",integer2);

            //本月新增会员数
            QueryWrapper<Member> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.between("regTime",day4ThisMonth,today);
            Integer integer3 = memberMapper.selectCount(queryWrapper2);
            resultMap.put("thisMonthNewMember",integer3);

            //今日新增预约人数
            QueryWrapper<Order> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("orderDate",today);
            Integer integer4 = orderMapper.selectCount(queryWrapper3);
            resultMap.put("todayOrderNumber",integer4);

            //今日到诊人数
            queryWrapper3.eq("orderStatus","已到诊");
            Integer integer5 = orderMapper.selectCount(queryWrapper3);
            resultMap.put("todayVisitsNumber",integer5);

            //本周新增预约人数
            QueryWrapper<Order> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.between("orderDate",thisWeekMonday,today);
            Integer integer6 = orderMapper.selectCount(queryWrapper4);
            resultMap.put("thisWeekOrderNumber",integer6);
            //本周到诊人数
            queryWrapper4.eq("orderStatus","已到诊");
            Integer integer7 = orderMapper.selectCount(queryWrapper4);
            resultMap.put("thisWeekVisitsNumber",integer7);


            //本月新增预约人数
            QueryWrapper<Order> queryWrapper5 = new QueryWrapper<>();
            queryWrapper5.between("orderDate",day4ThisMonth,today);
            Integer integer8 = orderMapper.selectCount(queryWrapper5);
            resultMap.put("thisMonthOrderNumber",integer8);
            //本月到诊人数
            queryWrapper5.eq("orderStatus","已到诊");
            Integer integer9 = orderMapper.selectCount(queryWrapper5);
            resultMap.put("thisMonthVisitsNumber",integer9);

            //热门套餐
            List<Map<String,Object>> list = setmealMapper.getHostSetmealInfo();
            resultMap.put("hotSetmeal",list);

        }catch (Exception e){
            e.printStackTrace();
        }

        return new Result(true,"",resultMap);
    }
}
